package com.wry.wry_test.feign.config;

import java.util.Random;

/**
 * @Author: wangrongyi
 * @Date: 2024/7/24 17:28
 * @Description: 随机算法
 */
public class RandomAlgo implements LoadBalanceService {
    private final LoadBalanceProperties properties;

    public RandomAlgo(LoadBalanceProperties loadBalanceProperties) {
        this.properties = loadBalanceProperties;
    }

    @Override
    public synchronized String getServerAddress() {
        if (properties.getAddress().isEmpty()) {
            throw new RuntimeException("服务地址为空");
        }
        int index = new Random().nextInt(properties.getAddress().size());
        return properties.getAddress().get(index);
    }
}
